package com.by.controller;

import com.by.entity.Book;
import com.by.service.BookService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.annotation.Resource;

/**
 * (Book)表控制层
 *
 * @author 白也
 * @since 2024-01-16 17:50:59
 */
@RestController
@RequestMapping("book")
public class BookController {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    /**
     * 分页查询
     *
     * @param pageNum  第几页
     * @param pageSize 分页大小
     * @return 查询结果
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public ResponseEntity<List<Book>> queryByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        return ResponseEntity.ok(this.bookService.queryAllByLimit(pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Book> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bookService.queryById(id));

    }

    /**
     * 新增数据
     *
     * @param book 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Book> add(Book book) {
        return ResponseEntity.ok(this.bookService.insert(book));
    }

    /**
     * 编辑数据
     *
     * @param book 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Book> edit(Book book) {
        return ResponseEntity.ok(this.bookService.update(book));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.bookService.deleteById(id));
    }

}

